package com.willren;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class Configure {
	public static final String TABLE_NAME = "ap_setting";
	public static final String DATABASE_NAME = "/data/data/com.willren/TestHotSpot.db";
	
	public static final String DEFAULT_AP_NAME = "DefaultAp";
	
	private String apName;
	private String apPassword;
	private boolean isEncrypted;
	
	private static Configure objConfigure;
	
	public static Configure getInstance() {
		if (null == objConfigure) {
			objConfigure = new Configure();
		}
		return objConfigure;
	}
	
	private Configure() {
		readConfigure();
	}
	
	protected boolean readConfigure() {
		boolean ret = false;
		
		SQLiteDatabase database = null;
		Cursor cur = null;
		
		try {
			database = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null);
		} catch(SQLiteException e) {
			Log.i("sql_error", e.toString());
		}
		
		if (null != database) {
			try {
				cur = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
			} catch(SQLiteException e) {
				Log.i("sql_error", e.toString());
			}
			
			if (null != cur && cur.moveToFirst()) {
				this.apName = cur.getString(cur.getColumnIndex("ap_name"));
				this.isEncrypted = (1 == cur.getInt(cur.getColumnIndex("is_encrypted")));
				this.apPassword = cur.getString(cur.getColumnIndex("ap_password"));
				
				ret = true;
			}
			else {
				this.apName = DEFAULT_AP_NAME;
				this.isEncrypted = false;
				this.apPassword = null;
			}
			
			database.close();
		}
		
		return ret;
	}

	protected boolean writeConfigure() {
		boolean ret = false;
		
		SQLiteDatabase database = null;
		
		try {
			database = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null);
		} catch(SQLiteException e) {
			Log.i("sql_error", e.toString());
		}
		
		if (null != database) {
			try {
				database.execSQL("DROP TABLE " + TABLE_NAME);
				
				database.execSQL("CREATE TABLE " + TABLE_NAME + 
						"(ap_name VARCHAR, is_encrypted int, ap_password VARCHAR)");
				
				if (isEncrypted) {
					database.execSQL("INSERT INTO " + TABLE_NAME + "(ap_name, is_encrypted, ap_password)" + 
						"VALUES(\"" + this.apName + "\", 1, \"" + this.apPassword + "\")");
				}
				else {
					database.execSQL("INSERT INTO " + TABLE_NAME + "(ap_name, is_encrypted, ap_password)" + 
						"VALUES(\"" + this.apName + "\", 0, \"\")");
				}
			} catch(SQLiteException e) {
				Log.i("sql_error", e.toString());
			}
			
			database.close();
		}
		
		return ret;
	}
	
	public String getApName() {
		return apName;
	}

	public void setApName(String apName) {
		this.apName = apName;
		
		writeConfigure();
	}

	public boolean isEncrypted() {
		return isEncrypted;
	}

	public void setEncrypted(boolean isEncrypted) {
		this.isEncrypted = isEncrypted;
		
		writeConfigure();
	}

	public String getApPassword() {
		return apPassword;
	}

	public void setApPassword(String apPassword) {
		this.apPassword = apPassword;
		
		writeConfigure();
	}
}
